<?php
/**
 * MDB2 Aborior's Simplex driver
 *
 * PHP versions 5
 *
 * LICENSE:
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. The name of the authors may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * @category  Database
 * @package   MDB2_Driver_simplex
 * @author    Philippe Jausions <jausions@php.net>
 * @copyright 1998-2008 Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, Lukas Smith, Philippe Jausions
 * @license   http://www.opensource.org/licenses/bsd-license.php BSD License
 * @version   CVS: $Id: $
 * @link      http://pear.11abacus.com/package/MDB2_Driver_simplex
 */

require_once 'MDB2/Driver/Manager/Common.php';

/**
 * MDB2 Aborior's Simplex driver for the management modules
 *
 * @category Database
 * @package  MDB2_Driver_simplex
 * @author   Philippe Jausions <jausions@php.net>
 */
class MDB2_Driver_Manager_simplex extends MDB2_Driver_Manager_Common
{
    /**
     * Creation of a new table is not supported
     *
     * @param string $name    Name of the database to be created
     * @param array  $fields  Not supported
     * @param array  $options Not supported
     *
     * @return mixed MDB2 error
     */
    public function createTable($name, $fields, $options = array())
    {
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            'method not implemented', __FUNCTION__);
    }

    /**
     * Altering an existing table is not supported
     *
     * @param string  $name    Name of the table to be changed.
     * @param array   $changes Not supported
     * @param boolean $check   Not supported  
     * 
     * @return mixed a MDB2 error 
     */
    public function alterTable($name, $changes, $check)
    {
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            'method not implemented', __FUNCTION__);
     }

    /**
     * Lists all databases
     *
     * @return array List of database names on success, a MDB2 error on failure
     */
    public function listDatabases()
    {
        $db =& $this->getDBInstance();
        if (PEAR::isError($db)) {
            return $db;
        }

        try  {
            $result = $db->getConnection()->listDatabases();
        } catch (Exception $e) {
            $error = PEAR::raiseError($e->getMessage());
            return $error;
        }
        if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
            $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
        }
        return $result;
    }

    /**
     * Lists all users
     *
     * @return array List of user names on success, a MDB2 error on failure
     */
    public function listUsers()
    {
        return array();
    }

    /**
     * Lists all functions in the current database
     *
     * @return array List of function names on success, a MDB2 error on failure
     */
    public function listFunctions()
    {
        return array();
    }

    /**
     * List all triggers in the database that reference a given table
     *
     * @param string $table table name for which all referenced triggers should 
     *                      be returned
     * 
     * @return array List of trigger names on success, a MDB2 error on failure
     */
    function listTableTriggers($table = null)
    {
        return array();
    }

    /**
     * Lists all tables in the current database
     *
     * @param string $database Database name (defaults to the current)
     * 
     * @return array List of table names on success, a MDB2 error on failure
     */
    public function listTables($database = null)
    {
        $db =& $this->getDBInstance();
        if (PEAR::isError($db)) {
            return $db;
        }

        try  {
            $prevDB = null;
            $conn   = $db->getConnection();
            if ($database !== null) {
                $prevDB = $conn->selectDatabase($database);
            }
            $result = $conn->listTables();
            if ($prevDB !== null) {
                $conn->selectDatabase($prevDB);
            }
        } catch (Exception $e) {
            $error = PEAR::raiseError($e->getMessage());
            return $error;
        }
        if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
            $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
        }
        return $result;
    }

    /**
     * List all views in the current database
     *
     * @param string $database Database name (defaults to the current)
     * 
     * @return array List of view names on success, a MDB2 error on failure
     */
    public function listViews($database = null)
    {
        return array();
    }

    /**
     * List all fields in a table in the current database
     *
     * @param string $table name of table that should be used in method
     * 
     * @return array list of field names on success, a MDB2 error on failure
     */
    public function listTableFields($table)
    {
        $db =& $this->getDBInstance();
        if (PEAR::isError($db)) {
            return $db;
        }
        try  {
            $result = $db->getConnection()->listFields($table);        
        } catch (Exception $e) {
            $error = PEAR::raiseError($e->getMessage());
            return $error;
        }

        if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
            $result = array_map(($db->options['field_case'] == CASE_LOWER 
                                ? 'strtolower' : 'strtoupper'), 
                                array_keys($result));
        }
        return $result;
    }

    /**
     * Creating an index is not supported
     *
     * @param string $table      Name of the table on which the index is to be created
     * @param string $name       Name of the index to be created
     * @param array  $definition Not supported
     *   
     * @return mixed a MDB2 error on failure
     */
    public function createIndex($table, $name, $definition)
    {
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            'method not implemented', __FUNCTION__);
    }

    /**
     * Dropping an existing index is not supported
     *
     * @param string $table Name of table that should be used in method
     * @param string $name  Name of the index to be dropped
     * 
     * @return mixed a MDB2 error on failure
     */
    function dropIndex($table, $name)
    {
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            'method not implemented', __FUNCTION__);
    }

    /**
     * List all indexes in a given table
     *
     * @param string $table name of table that should be used in method
     * 
     * @return array List of index names on success, a MDB2 error on failure
     */
    public function listTableIndexes($table)
    {
        $db =& $this->getDBInstance();
        if (PEAR::isError($db)) {
            return $db;
        }

        try  {
            $result = $db->getConnection()->listIndexes($table);
        } catch (Exception $e) {
            $error = PEAR::raiseError($e->getMessage());
            return $error;
        }
        if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
            $result = array_change_key_case($result, $db->options['field_case']);
        }

        //echo __FILE__.' ['.__LINE__."] \n";
        //print_r($result);
        return $result;
    }

    /**
     * Creating a constraint on a table is not supported
     *
     * @param string $table      Name of the table on which the constraint is to be created
     * @param string $name       Name of the constraint to be created
     * @param array  $definition Unsupported
     * 
     * @return mixed a MDB2 error on failure
     */
    public function createConstraint($table, $name, $definition)
    {
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            'method not implemented', __FUNCTION__);
    }

    /**
     * Dropping an existing constraint is not supported
     *      
     * @param string $table   Name of table that should be used in method
     * @param string $name    Name of the constraint to be dropped
     * @param string $primary Hint if the constraint is primary
     * 
     * @return mixed a MDB2 error on failure
     */
    public function dropConstraint($table, $name, $primary = false)
    {
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            'method not implemented', __FUNCTION__);
    }

    /**
     * Lists all constraints on a table
     *
     * @param string $table name of table that should be used in method
     * 
     * @return array List of constraint names on success, a MDB2 error on failure
     */
    function listTableConstraints($table)
    {
        return $this->listTableIndexes($table);
    }

    /**
     * Creating a sequence is not supported
     *
     * @param string $seq_name Name of the sequence to create
     * @param string $start    Start value of the sequence; default is 1
     * @param array  $options  Unsupported
     * 
     * @return mixed a MDB2 error on failure
     */
    public function createSequence($seq_name, $start = 1, $options = array())
    {
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            'method not implemented', __FUNCTION__);
    }

    /**
     * Dropping an existing sequence is not supported
     *
     * @param string $seq_name Name of the sequence to drop
     * 
     * @return mixed a MDB2 error on failure
     */
    public function dropSequence($seq_name)
    {
        return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
            'method not implemented', __FUNCTION__);
    }

    /**
     * Lists all sequences in the current database (not implemented)
     *
     * @param  string $database Database name (defaults to current)
     * 
     * @return mixed array of sequence names on success, a MDB2 error on failure
     * @todo   Should be able to find sequences in the tables
     */
    public function listSequences($database = null)
    {
        return array();
    }
}

?>